﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ScrumTable.Common.Monitoring;
using ScrumTable.Common.Security;
using ScrumTable.Common.Threading;
using ScrumTable.DL.Data.Common;
using ScrumTable.DL.Data.Generic.Test.Mocks;
using ScrumTable.DL.Data.Generic.Test.Mocks.Config.Driver;
using ScrumTable.DL.Data.Generic.Test.Mocks.Config.Schema;
using ScrumTable.DL.Data.Local.Config.Driver;
using ScrumTable.DL.Data.Local.Config.Schema;

#endregion


namespace ScrumTable.DL.Data.Local.Test
{
    /// <summary>
    /// This is the test class for Local.DataConnection class.
    /// </summary>
    [TestClass]
    public class DataConnectionTest
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private DataContext _context;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        #endregion

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new DataConnectionTest instance.
        /// </summary>
        public DataConnectionTest()
        {
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Use TestInitialize to run code before running each test .
        /// </summary>
        [TestInitialize]
        public void TestInitialize()
        {
            _context = new DataContext(new DataContextInfoStub(
                TestContext.TestDir,
                dci => new DataSchemaSelector(dci),
                dci => new DriverConfigSelector(dci)));
        }

        /// <summary>
        /// Use TestCleanup to run code after each test has run.
        /// </summary>
        [TestCleanup]
        public void TestCleanup()
        {
            _context.Dispose();
            _context = null;
        }

        [TestMethod]
        public void TestConnect()
        {
            bool callBackCalled = false;
            _context.Connection.Connect(
                new ConnectionInformationManager(Resources.Member1Credentials),
                r =>
                {
                    callBackCalled = true;
                    Assert.IsNull(((IDataResult)r).Error);
                },
                Resources.GetProjectTestDataFilePath(TestContext.TestDir));
            Assert.IsTrue(callBackCalled);
        }

        [TestMethod]
        public void TestChangeUser()
        {
            _context.Connection.Connect(
                new ConnectionInformationManager(Resources.Member1Credentials, Resources.Member2Credentials),
                result => { },
                Resources.GetProjectTestDataFilePath(TestContext.TestDir));

            Assert.AreEqual(Resources.Member1Credentials, _context.Connection.ConnectionManager.Selected);
            bool userChanged = false;
            IDataObject memberObject = _context.Storage[_context.Schema.MemberSchema].GetById(Resources.Member2IdChain, Resources.Member2Id);
            _context.ImportData(Resources.FullProject1IdChain, new NullDataImportMonitor());
            _context.Connection.ChangeUser(
                memberObject,
                rawResult =>
                    {
                        ICancellableDataResult result = (ICancellableDataResult)rawResult;
                        Assert.IsTrue(result.DataAccepted);
                        userChanged = true;
                    });
            Assert.IsTrue(userChanged);
            Assert.AreEqual(Resources.Member2Credentials, _context.Connection.ConnectionManager.Selected);
        }

        [TestMethod]
        public void TestForceChangeUser()
        {
            _context.Connection.Connect(
                new ConnectionInformationManager(Resources.Member1Credentials, Resources.Member2Credentials),
                result => { },
                Resources.GetProjectTestDataFilePath(TestContext.TestDir));

            Assert.AreEqual(Resources.Member1Credentials, _context.Connection.ConnectionManager.Selected);
            IDataObject memberObject = _context.Storage[_context.Schema.MemberSchema].GetById(Resources.Member2IdChain, Resources.Member2Id);
            _context.ImportData(Resources.FullProject1IdChain, new NullDataImportMonitor());
            _context.Connection.ChangeUser(memberObject);
            Assert.AreEqual(Resources.Member2Credentials, _context.Connection.ConnectionManager.Selected);
        }


        [TestMethod]
        public void TestChangeUserWithCancel()
        {
            _context.Connection.Connect(
                new ConnectionInformationManager(Resources.Member1Credentials, Resources.Member2Credentials),
                result => { },
                Resources.GetProjectTestDataFilePath(TestContext.TestDir));

            bool memberChanged = false;
            IDataObject memberObject = _context.Storage[_context.Schema.MemberSchema].GetById(Resources.Member2IdChain, Resources.Member2Id);
            _context.ImportData(Resources.FullProject1IdChain, new NullDataImportMonitor());
            _context.Connection.ChangeUser(
                memberObject,
                rawResult =>
                    {
                        ((ICancellableDataResult)rawResult).Cancel();
                        memberChanged = true;
                    });
            Assert.IsTrue(memberChanged);
            Assert.AreEqual(Resources.Member1Credentials, _context.Connection.ConnectionManager.Selected);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void TestChangeUserWithException()
        {
            _context.Connection.Connect(
                new ConnectionInformationManager(Resources.Member1Credentials),
                result => { },
                Resources.GetProjectTestDataFilePath(TestContext.TestDir));

            IDataObject memberObject = _context.Storage[_context.Schema.MemberSchema].GetById(Resources.Member2IdChain, Resources.Member2Id);
            _context.ImportData(Resources.FullProject1IdChain, new NullDataImportMonitor());
            _context.Connection.ChangeUser(memberObject, rawResult => { }); // throws InvalidOperationException -> member cannot log in
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}
